package com.czk.utlls;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/3 15:55
 */
@Component
public class GetUserInfoUtil {
    @Value("${getUserInfoUrl}")
    private String getUserInfoUrl;
    @Autowired
    private TokenUtil tokenUtil;

    public String getUser(String openId) throws IOException {
        getUserInfoUrl = getUserInfoUrl.replace("ACCESS_TOKEN", tokenUtil.getToken().getAccessToken()).replace("OPENID", openId);
        // 发起get请求
        URL url = new URL(getUserInfoUrl);
        // 打开连接
        URLConnection urlConnection = url.openConnection();
        // 读数据
        InputStream inputStream1 = urlConnection.getInputStream();
        int len = 0;
        byte[] b = new byte[1024];
        StringBuilder resp = new StringBuilder();

        while ((len = inputStream1.read(b)) != -1) {
            resp.append(new String(b, 0, len));
        }
        return resp.toString();
    }
}
